// pages/cart/index.js.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    values: 1,
    value: 1,
    //控制隐藏显示
    buttomshow: [{
      checks: true,
      value: 0
    }, {
      checks: true,
      value: 0
    },{
      checks: true,
      value: 0
    },{
      checks: true,
      value: 0
    },{
      checks: true,
      value: 0
    }],
    gooodlist: [],
    nums: {
      goodnum: 1,
      goodnums: 1,
      goodnumss: 1,
      goodnumssss: 1
    },
    prices: [],
    pricenums: 0,
    //三个商品的计算价格
    numpr: 0,
    numpt: 0,
    numpts:0,
    numptss:0,
    upmoney: 600, //升级的钱
    judge: 0,
    payData: [],
    countOnChanges: 0,
    countOnChange: 0,
    countOnChangess: 0,
    show: false,
    showAddOrder: false,
    money: 0,
    //晋升目标
    upgradeLevel:0,
    //计算原价
    oldallori:0,
    count:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    this.setData({
      money: options.money,
    })
    this.getcount()
    wx.request({
      url: app.globalData.url+'/zkxj/good/listbydiscount',
      data: {
        "currPage": 1,
        "pageSize": 10,
        "goodArea":1,
        "putawayStatus": 1,
        "goodAreaDetail":1,
	        "level":1,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
          'token':wx.getStorageSync('token')
      },
      success: (res) => {
        this.setData({
          gooodlist: res.data.data.list,
        });
        console.log(this.data.gooodlist);
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
// 请求零售区数量
getcount(){
  wx.request({
    url: app.globalData.url+'/zkxj/cart/area/countGood',
    data: {
      "goodAreaDetail": 1,
      "userId": wx.getStorageSync('userId'),
    },
    method: 'POST',
    header: {
      'content-type': 'application/json',
        'token':wx.getStorageSync('token')
    },
    success: (res) => {
      console.log(res);
      this.setData({
        count: res.data.count,
      });
      console.log(this.data.gooodlist);
    },
  })
},
//添加
handleaddcart(e){
    const index = e.currentTarget.dataset['index'];
    const price = e.currentTarget.dataset['price'];
    console.log(index,price);
    const oldprice = e.currentTarget.dataset['oldpeice'];
wx.request({
  url: app.globalData.url+'/zkxj/cart/area/addNewGood',
  data: {
    "goodAreaDetail": 1,
	"goodId": index,
	"modifiedtime": "",
	"num": 1,
	"oldPrice": oldprice,
	"price": price,
	"userId": wx.getStorageSync('userId')
  },
  method: 'POST',
  header: {
    'content-type': 'application/json',
      'token':wx.getStorageSync('token')
  },
  success: (res) => {
    console.log(res);
   if(res.data.msg=="商品添加成功！"){
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 2000,
      mask: true
    })
    this.getcount()
   }else{
    wx.showToast({
      title: res.data.msg,
      icon: 'error',
      duration: 2000,
      mask: true
    })
   }
  },
})
  },
  handletorecart(){
    wx.navigateTo({
      url: `/pages/retcart/index` 
  })
  },
  //去结算
  //去结算
  handletobuy() {
    // if (this.data.money != this.data.numpr + this.data.numpt + this.data.numpts +this.data.pricenums) {
    //   wx.showToast({
    //     title: '选购金额不符',
    //     icon: 'error',
    //     duration: 1000,
    //     mask: true
    //   })
    // } else {
      var that = this.data
      if (this.data.payData.length == 0) {
        this.setData({
          showAddOrder: true
        });
        return
      }
      var sumMoney = that.numpr + that.numpt+ that.numpts + that.pricenums //总价
	  var detailsData= JSON.stringify(this.data.payData);
		detailsData = encodeURIComponent(detailsData);
      wx.navigateTo({
          url: `/pages/seaccounts/index?arr=${detailsData}&sum=${JSON.stringify(sumMoney)}&type=3&upgradeLevel=${this.data.upgradeLevel}` 
      })
      console.log(this.data.payData);
    // }
  },
  onCloses() {
    this.setData({
      show: false
    });
    this.setData({
      showAddOrder: false
    });
  },
 //失去焦点
 bluerchange(){
  // this.setData({
  //   nums: {
  //     goodnum: 0, //数量
  //   },
  // })
  console.log(this.data.nums.goodnum);
 },
  handlecheckbu(e) {
    var that = this.data
    const index = e.currentTarget.dataset['index'];
    const price = e.currentTarget.dataset['price'];
    console.log(price);
    console.log(index);
    this.data.buttomshow[index].checks = false
    this.setData({
      buttomshow: this.data.buttomshow,
      prices: this.data.price,
      pricenums: this.data.pricenums + price
    });
    var count = that.buttomshow[index].value
    var money = that.pricenums
    if (index == 0) {
      that.payData.push({
        id: index + '',
        num: count || 1,
        oldpric:that.gooodlist[0].originalPrice,
        price: this.data.numpr||that.upgradeLevel==2?that.gooodlist[0].originalPrice:that.gooodlist[0].originalPrice,
        name: that.gooodlist[0].name,
        coverPic: that.gooodlist[0].coverPic
      })
    } else if(index == 1) {
      that.payData.push({
        id: index + '',
        num: count || 1,
        oldpric:that.gooodlist[1].originalPrice,
        price: this.data.numpt||that.upgradeLevel==2?that.gooodlist[1].originalPrice:that.gooodlist[1].originalPrice,
        name: that.gooodlist[1].name,
        coverPic: that.gooodlist[1].coverPic
      })
    } else if(index == 3) {
      that.payData.push({
        id: 7 + '',
        num: count || 1,
        oldpric:that.gooodlist[4].originalPrice,
        price: this.data.numpt||that.upgradeLevel==2?that.gooodlist[4].originalPrice:that.gooodlist[4].originalPrice,
        name: that.gooodlist[4].name,
        coverPic: that.gooodlist[4].coverPic
      })
    }else{
      that.payData.push({
        id: index + '',
        num: count || 1,
        oldpric:that.gooodlist[2].originalPrice,
        price: this.data.numpts||that.upgradeLevel==2?that.gooodlist[2].originalPrice:that.gooodlist[2].originalPrice,
        name: that.gooodlist[2].name,
        coverPic: that.gooodlist[2].coverPic
      })
    }
    console.log(this.data.payData);
  },
  onChange(event) {
    var that = this.data
    this.setData({
      nums: {
        goodnum: event.detail, //数量
      },
      numpr: (event.detail - 1)*that.gooodlist[0].originalPrice, //价格
      oldallori:(event.detail - 1) * that.gooodlist[0].originalPrice
      // 原价
    })
   console.log(this.data.numpr);
    if (that.nums.goodnum == 0) {
      that.countOnChange = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '0') {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChange == 1) {
        that.payData.push({
          id: '0',
          num: 1,
          oldpric:that.gooodlist[0].originalPrice,
          price: that.upgradeLevel==2?that.gooodlist[0].originalPrice:that.gooodlist[0].originalPrice,
          name: that.gooodlist[0].name,
          coverPic: that.gooodlist[0].coverPic
        })
        that.countOnChange = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '0') {
          that.payData[i].num = that.nums.goodnum
          that.payData[i].price = (event.detail) * that.upgradeLevel==2?that.gooodlist[0].originalPrice:that.gooodlist[0].originalPrice
        }
      }
      // console.log(that.payData)
    }
  },
  onChanges(event) {
    var that = this.data
    this.setData({
      nums: {
        goodnums: event.detail
      },
      numpt: that.upgradeLevel==2?(event.detail - 1) * that.gooodlist[1].originalPrice:(event.detail - 1) * that.gooodlist[1].originalPrice
    })
   
    if (that.nums.goodnums == 0) {
      that.countOnChanges = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == 1) {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChanges == 1) {
        that.payData.push({
          id: '1',
          num: 1,
          oldpric:that.gooodlist[1].originalPrice,
          price: that.upgradeLevel==2?that.gooodlist[1].originalPrice:that.gooodlist[1].originalPrice,
          name: that.gooodlist[1].name,
          coverPic: that.gooodlist[1].coverPic
        })
        that.countOnChanges = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '1') {
          that.payData[i].num = that.nums.goodnums
          that.payData[i].price = (event.detail) * that.upgradeLevel==2?that.gooodlist[1].originalPrice:that.gooodlist[1].originalPrice
        }
      }
      // console.log(that.payData)
    }
  },
  onChangess(event) {
    var that = this.data
    this.setData({
      nums: {
        goodnumss: event.detail
      },
      numpts: (event.detail - 1) * that.gooodlist[2].originalPrice
    })
    console.log(this.data.numpt);
   
    if (that.nums.goodnumss == 0) {
      that.countOnChangess = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == 2) {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChangess == 1) {
        that.payData.push({
          id: '2',
          num: 1,
          oldpric:that.gooodlist[2].originalPrice,
          price:that.gooodlist[2].originalPrice,
          name: that.gooodlist[2].name,
          coverPic: that.gooodlist[2].coverPic
        })
        that.countOnChangess = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '2') {
          that.payData[i].num = that.nums.goodnumss
          that.payData[i].price = that.gooodlist[2].originalPrice
        }
      }
      // console.log(that.payData)
    }
  },
  onChangessss(event) {
    var that = this.data
    this.setData({
      nums: {
        goodnumssss: event.detail
      },
      numptss: (event.detail - 1) * that.gooodlist[4].originalPrice
    })
    console.log(this.data.numpt);
   console.log(that.payData);
    if (that.nums.goodnumssss == 0) {
      that.countOnChangess = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == 3) {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChangess == 1) {
        that.payData.push({
          id: '7',
          num: 1,
          oldpric:that.gooodlist[4].originalPrice,
          price:that.gooodlist[4].originalPrice,
          name: that.gooodlist[4].name,
          coverPic: that.gooodlist[4].coverPic
        })
        that.countOnChangess = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '7') {
          that.payData[i].num = that.nums.goodnumssss
          that.payData[i].price = that.gooodlist[4].originalPrice
        }
      }
      // console.log(that.payData)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})